<?php

/* SMILEYS START */
$smileys = array( 

	':)'			=>	'smile.png',
	';)'			=>	'wink.png',
	':D'			=>	'big-smile.png',
	':('			=>	'sad.png',
	":'("			=>	'crying.png',
	":p"			=>	'tongue.png',
	'<3<3'			=>	'love.png',
	':*'			=>	'kiss.png',
	':|'			=>	'straight-face.png',
	'3-|'			=>	'not-interested.png',
	':s'			=>	'confused.png',
	
	">:O"			=>	'angry.png',
	":$"			=>	'embarrassed.png',
	":O"			=>	'surprised.png',
	"(=|"			=>	'yawn.png',
	":x"			=>	'zipped.png',
	">=)"			=>	'devil.png',
	"B-)"			=>	'cool.png',
	":nerd:"		=>	'nerd.png',
	":whistle:"		=>	'whistle.png',
	":grin:"		=>	'grin.png',
	":sarcasm:"		=>	'sarcasm.png',
	":impatient:"	=>	'impatient.png',
	":sour:"		=>	'sour.png',
	":shocked:"		=>	'shocked.png',
	":sing:"		=>	'sing.png',
	":smug:"		=>	'smug.png',
	":stress:"		=>	'stress.png',
	":silly:"		=>	'silly.png',
	":mad:"			=>	'mad.png',
	":dead:"		=>	'dead.png',
	":smitten:"		=>	'smitten.png',
	
	":evil:"		=>	'evil.png'

);
//':&'			=>	'sick.png',
/* SMILEYS END */

$imagePathFromRoot="cometchat/";

function getSmileyText()
{
	global $smileys;
	global $imagePathFromRoot;
	$smileyText="";
	foreach ($smileys as $smileypattern => $smileyresult) 
	{
		$smileypattern2 = str_replace("'","\\'",$smileypattern);
		$smileytitle = str_replace("-"," ",ucwords(preg_replace("/\.(.*)/","",$smileyresult)));
		$smileyText .= '<img height="20" src="'.$imagePathFromRoot.'images/smileys/'.$smileyresult.'" title="'.$smileypattern.' ('.$smileytitle.')" onclick="addSmileyText(\''.$smileypattern2.'\');" style="margin:2px">';
	}
	return $smileyText;
}

function getSmileyDataForSave($messageText,$jsonReq=false)
{
	global $smileys;
	global $imagePathFromRoot;
	
	$arrKey=array();
	$arrValue=array();
	$pendingSmiley=array();
	
    //$messageText=convertYouTubeLink($messageText);
    $messageText=hyperlinksAnchored($messageText);
	foreach ($smileys as $smileypattern => $smileyresult) 
	{
		if($smileypattern!=':s')
		{
			$arrKey[]=$smileypattern;
			$smileypattern2 = str_replace("'","\\'",$smileypattern);
			$smileytitle = str_replace("-"," ",ucwords(preg_replace("/\.(.*)/","",$smileyresult)));
            $smileyText = '<img height="20" src="'.$imagePathFromRoot.'images/smileys/'.$smileyresult.'" title="'.$smileytitle.'" style="margin:2px">';
            if($jsonReq){
                $smileyText =str_replace("\"","REP_DBLQUOTE",$smileyText);
            }
			$arrValue[]=$smileyText;
		}
		else
		{
			$pendingSmiley[$smileypattern]=$smileyresult;
		}
	}
	$messageText=str_replace($arrKey,$arrValue,$messageText);
	
	$arrKey=array();
	$arrValue=array();
	foreach ($pendingSmiley as $smileypattern => $smileyresult) 
	{
		$arrKey[]=$smileypattern;
		$smileypattern2 = str_replace("'","\\'",$smileypattern);
		$smileytitle = str_replace("-"," ",ucwords(preg_replace("/\.(.*)/","",$smileyresult)));
		$smileyText = '<img height="20" src="'.$imagePathFromRoot.'images/smileys/'.$smileyresult.'" title="'.$smileytitle.'" style="margin:2px">';
        if($jsonReq){
                $smileyText =str_replace("\"","REP_DBLQUOTE",$smileyText);
        }
		$arrValue[]=$smileyText;
	}
	$messageText=str_replace($arrKey,$arrValue,$messageText);
    
    
	return $messageText;
}

function hyperlinksAnchored($text) {
    //$text="www.youtube.com/watch?v=vIaH35-MLsk  test www.youtube.com/watch?v=vIaH35-MLsk1 ";
    //return preg_replace('@(http)?(s)?(://)?(([-\w]+\.)+([^\s]+)+[^,.\s])@', verifyAndPlaceLink('$1$2$3$4'), $text);
    return verifyAndPlaceLink($text);
    
}

function verifyAndPlaceLink($text){
    $str="";
    $matches=array();
    preg_match_all('@(http)?(s)?(://)?(([-\w]+\.)+([^\s]+)+[^,.\s])@', $text,$matches);
    $linkArr=$matches[0];
    //print_r($linkArr);
    foreach($linkArr as $linkCheck){
        
        if(!strstr($linkCheck,"youtube.com/watch") && !strstr($linkCheck,"@")){
            
            $nlinkCheck=$linkCheck;
            if(!strstr($linkCheck,"http://") && !strstr($linkCheck,"https://")){
                $nlinkCheck="http://".$linkCheck;
            }
            //echo $linkCheck;
            $alinkCheck='<a href="'.$nlinkCheck.'" class="linktxt" target="_blank">'.$nlinkCheck.'</a>';
            //echo $text." == ".$linkCheck." = ".$alinkCheck;
            //die();
            //$text=str_replace($linkCheck,"<a href='$linkCheck' class='linktxt' target='_blank'>$linkCheck</a>",$text);
            $text=str_replace($linkCheck,$alinkCheck,$text);
        }
        else{
            $text=str_replace($linkCheck,preg_replace("/\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i","<br/><br/><center><iframe width=\"420\" height=\"315\" src=\"http://www.youtube.com/embed/$1\" frameborder=\"0\" allowfullscreen></iframe></center><br/>",$linkCheck),$text);
            //$str="<br/><iframe width=\"420\" height=\"315\" src=\"http://www.youtube.com/embed/$1\" frameborder=\"0\" allowfullscreen></iframe>";
        }    
    }
    $str=$text;
    
    return $str;
}

function convertYouTubeLink($text){
    return preg_replace("/\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i","<br/><iframe width=\"420\" height=\"315\" src=\"http://www.youtube.com/embed/$1\" frameborder=\"0\" allowfullscreen></iframe>",$text);
}
?>